<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Rust-Cool-Nginx - 监控面板</title>
    <link rel="stylesheet" href="css/style.css">
    <style>
        .navbar {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            padding-top: 0.5rem;
            padding-bottom: 0.5rem;
            background: rgba(30, 30, 40, 0.7);
            box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
            border: 1px solid rgba(0, 255, 255, 0.2);
            border-radius: 10px;
            margin: 20px;
        }

        .navbar > .container {
            display: flex;
            flex-wrap: inherit;
            align-items: center;
            justify-content: space-between;
        }

        .navbar-brand {
            padding-top: 0.3125rem;
            padding-bottom: 0.3125rem;
            margin-right: 1rem;
            font-size: 1.25rem;
            text-decoration: none;
            white-space: nowrap;
            font-weight: 700;
            color: var(--primary);
            background: linear-gradient(90deg, var(--gradient-start), var(--gradient-end));
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
        }

        .navbar-nav {
            display: flex;
            flex-direction: row;
            padding-left: 0;
            margin-bottom: 0;
            list-style: none;
        }

        .nav-link {
            display: block;
            padding: 0.5rem 1rem;
            color: #ccc;
            text-decoration: none;
            transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out;
        }

        .nav-link:hover, .nav-link:focus {
            color: var(--primary);
        }

        .navbar-nav .nav-link.active {
            color: var(--primary);
            background: linear-gradient(90deg, var(--gradient-start), var(--gradient-end));
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
        }

        .navbar-collapse {
            flex-basis: 100%;
            flex-grow: 1;
            align-items: center;
        }

        .navbar-toggler {
            padding: 0.25rem 0.75rem;
            font-size: 1.25rem;
            line-height: 1;
            background-color: transparent;
            border: 1px solid transparent;
            border-radius: 0.375rem;
            transition: box-shadow 0.15s ease-in-out;
        }

        .dashboard-container {
            padding: 20px;
        }

        .card-container {
            display: flex;
            flex-wrap: wrap;
            gap: 20px;
            margin-bottom: 30px;
        }

        .card {
            background: rgba(30, 30, 40, 0.7);
            border: 1px solid rgba(0, 255, 255, 0.2);
            border-radius: 10px;
            padding: 20px;
            flex: 1;
            min-width: 200px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
        }

        .card-title {
            font-size: 1rem;
            color: #aaa;
            margin-bottom: 10px;
        }

        .card-value {
            font-size: 2rem;
            color: var(--primary);
            font-weight: bold;
        }

        .chart-container {
            background: rgba(30, 30, 40, 0.7);
            border: 1px solid rgba(0, 255, 255, 0.2);
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 30px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
        }

        .chart-title {
            font-size: 1.2rem;
            color: var(--primary);
            margin-bottom: 15px;
        }

        .chart-placeholder {
            height: 300px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #777;
            border: 1px dashed #555;
            border-radius: 5px;
        }

        .table-container {
            background: rgba(30, 30, 40, 0.7);
            border: 1px solid rgba(0, 255, 255, 0.2);
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
            overflow-x: auto;
        }

        .table-title {
            font-size: 1.2rem;
            color: var(--primary);
            margin-bottom: 15px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #444;
        }

        th {
            color: var(--primary);
            font-weight: bold;
        }

        tr:hover {
            background-color: rgba(0, 255, 255, 0.1);
        }

        .status-running {
            color: #0f0;
        }

        .status-stopped {
            color: #f00;
        }

        .btn-refresh {
            background: linear-gradient(90deg, var(--gradient-start), var(--gradient-end));
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            float: right;
            margin-bottom: 20px;
        }

        .btn-refresh:hover {
            transform: scale(1.05);
            box-shadow: 0 0 15px rgba(0, 219, 222, 0.4);
        }

        .clearfix::after {
            content: "";
            clear: both;
            display: table;
        }
    </style>
</head>
<body>
    <nav class="navbar">
        <div class="container">
            <a class="navbar-brand" href="index.html">Rust-Cool-Nginx</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span>Menu</span>
            </button>
            <div class="navbar-collapse" id="navbarNav">
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="index.html">首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="config.html">配置</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="status.html">监控</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="static-file.html">静态文件</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>
    <div class="container dashboard-container">
        <h1 class="section-title">监控面板</h1>
        
        <button id="refresh-btn" class="btn-refresh">刷新数据</button>
        <div class="clearfix"></div>
        
        <div class="card-container">
            <div class="card">
                <div class="card-title">总请求数</div>
                <div class="card-value" id="total-requests">0</div>
            </div>
            <div class="card">
                <div class="card-title">当前连接数</div>
                <div class="card-value" id="current-connections">0</div>
            </div>
            <div class="card">
                <div class="card-title">成功率</div>
                <div class="card-value" id="success-rate">0%</div>
            </div>
            <div class="card">
                <div class="card-title">服务器状态</div>
                <div class="card-value status-running" id="server-status">运行中</div>
            </div>
        </div>
        
        <div class="chart-container">
            <div class="chart-title">请求流量统计</div>
            <div class="chart-placeholder" id="traffic-chart">
                请求流量图表 (需要使用图表库实现)
            </div>
        </div>
        
        <div class="chart-container">
            <div class="chart-title">响应时间</div>
            <div class="chart-placeholder" id="response-time-chart">
                响应时间图表 (需要使用图表库实现)
            </div>
        </div>
        
        <div class="table-container">
            <div class="table-title">实时请求</div>
            <table>
                <thead>
                    <tr>
                        <th>时间</th>
                        <th>客户端IP</th>
                        <th>请求方法</th>
                        <th>请求路径</th>
                        <th>响应状态</th>
                        <th>响应时间(ms)</th>
                    </tr>
                </thead>
                <tbody id="requests-table-body">
                    <tr>
                        <td>2025-11-07 10:30:25</td>
                        <td>127.0.0.1</td>
                        <td>GET</td>
                        <td>/index.html</td>
                        <td>200</td>
                        <td>15</td>
                    </tr>
                    <tr>
                        <td>2025-11-07 10:30:24</td>
                        <td>127.0.0.1</td>
                        <td>GET</td>
                        <td>/css/style.css</td>
                        <td>200</td>
                        <td>8</td>
                    </tr>
                    <tr>
                        <td>2025-11-07 10:30:23</td>
                        <td>127.0.0.1</td>
                        <td>GET</td>
                        <td>/status</td>
                        <td>200</td>
                        <td>5</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>

    <script>
        // 检查是否在 Tauri 环境中运行
        const isTauri = !!window.__TAURI__;
        
        // 获取基础URL
        function getBaseURL() {
            if (window.location.origin === 'http://tauri.localhost') {
                return 'http://127.0.0.1';
            } else {
                return window.location.origin;
            }
        }
        
        // 获取监控数据
        function fetchMonitoringData() {
            const apiUrl = `${getBaseURL()}/nginx_status`;
            console.log('监控数据 API URL:', apiUrl);
            
            return fetch(apiUrl)
                .then(response => {
                    const contentType = response.headers.get('content-type');
                    if (contentType && contentType.includes('application/json')) {
                        if (!response.ok) {
                            throw new Error(`HTTP error! status: ${response.status}`);
                        }
                        return response.json();
                    } else {
                        throw new Error('监控数据 API 返回非 JSON 响应');
                    }
                })
                .then(data => {
                    console.log('通过API获取到的监控数据:', data);
                    updateDashboardWithData(data);
                })
                .catch(error => {
                    console.error('获取监控数据失败:', error);
                    // 使用模拟数据作为后备
                    updateDashboard();
                });
        }

        // 使用真实数据更新仪表板
        function updateDashboardWithData(data) {
            document.getElementById('total-requests').textContent = data.total_requests || 0;
            document.getElementById('current-connections').textContent = data.current_connections || 0;
            document.getElementById('success-rate').textContent = (data.success_rate ? data.success_rate.toFixed(1) : 0) + '%';
            
            // 更新服务器状态
            const statusElement = document.getElementById('server-status');
            statusElement.textContent = '运行中';
            statusElement.className = 'card-value status-running';
        }

        // 模拟监控数据更新
        function updateDashboard() {
            // 模拟数据
            const requests = Math.floor(Math.random() * 10000) + 5000;
            const connections = Math.floor(Math.random() * 100) + 50;
            const successRate = Math.floor(Math.random() * 30) + 70; // 70-99%
            
            document.getElementById('total-requests').textContent = requests;
            document.getElementById('current-connections').textContent = connections;
            document.getElementById('success-rate').textContent = successRate + '%';
        }

        // 页面加载完成后初始化
        document.addEventListener('DOMContentLoaded', function() {
            // 首次获取监控数据
            fetchMonitoringData();
            
            // 刷新按钮事件
            document.getElementById('refresh-btn').addEventListener('click', fetchMonitoringData);
            
            // 每5秒自动更新一次数据
            setInterval(fetchMonitoringData, 5000);
        });
    </script>
</body>
</html>